package com.yc.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.bean.User;
import com.yc.biz.UserBiz;
import com.yc.dao.DBHelper;

public class UserBizImp1 implements UserBiz {
	DBHelper db = new DBHelper();

	// 注册
		@Override
		public int reg(String uname, String pwd, String tel) {
			
			String sql = "insert into user(uid,uname,pwd,tel,isadmin) value(null,?,?,?,0)";
			List<Object> params = new ArrayList<Object>();
			params.add(uname);
			params.add(pwd);
			params.add(tel);

			int result = db.doUpdate(sql, params);
			
			//如果注册成功
			if(result>0){
				String sql1="select uid from user where uname='"+uname+"' and pwd='"+pwd+"' ";
				List<Map<String,String>> list=db.findAll(sql1, null);
				int uid=Integer.parseInt(list.get(0).get("uid")+"");
				String sql2="insert into payuseinfo value(null,uid,ptid,money)";
				for(int i=0;i<4;i++){
					sql2="insert into payuseinfo value(null,"+uid+","+(i+1)+",0)";
					int flag=db.doUpdate(sql2, null);
					if(flag<0){
						return 0;
					}
				}
				return 1;
			}else{
				return 0;
			}

		}

	// 登录
	@Override
	public User login(String uname, String pwd) {
		String sql = "select * from user where uname=? and pwd=?";
		List<Object> params = new ArrayList<Object>();
		params.add(uname);
		params.add(pwd);

		List<User> list = db.find(sql, params, User.class);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	// 后台的注册登录
	@Override
	public User adminLogin(String uname, String pwd) {
		String sql = "select * from user where uname=? and pwd=? and isadmin=1";
		List<Object> params = new ArrayList<Object>();
		params.add(uname);
		params.add(pwd);

		List<User> list = db.find(sql, params, User.class);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	// 管理员页面的用户信息修改
	@Override
	public int modify_user(int uid, String uname, String tel) {
		String sql = "UPDATE `user` SET uname=?,tel=? where uid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(uname);
		params.add(tel);
		params.add(uid);
		return db.doUpdate(sql, params);
	}

	@Override
	public int user_del(int uid) {
		String sql = "DELETE FROM `user` WHERE uid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(uid);
		return db.doUpdate(sql, params);
	}
	
	/**
	 * 管理员个人信息修改
	 * 用户个人信息修改
	 */
	@Override
	public int modify_person(int uid, String uname, String pwd,String paypwd, String tel) {
		String sql = "UPDATE `user` SET uname=?,pwd=?,tel=?,paypwd=? WHERE uid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(uname);
		params.add(pwd);
		params.add(tel);
		params.add(paypwd);
		params.add(uid);
		return db.doUpdate(sql, params);
	}

	// 获取管理员用户总页数
	@Override
	public int getUserTotalPage() {
		String sql = "SELECT (1) FROM `user`";
		int total = db.findAll(sql, null).size();
		int totalPage = total / 10 +1; // 30条数据 3页
		return totalPage;
	}

	// 管理员页面的用户分页查询
	@Override
	public List<User> getUsersbyPage(int page) {
		String sql = "select * from user limit " + (page - 1) * 10 + "," + 10;
		List<User> list = db.find(sql, null, User.class);
		return list;
	}
	
	@Override
	//管理员端的信息修改
	public int modify_persons(int uid, String uname, String pwd, String tel) {
		String sql = "UPDATE `user` SET uname=?,pwd=?,tel=? WHERE uid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(uname);
		params.add(pwd);
		params.add(tel);
		
		params.add(uid);
		return db.doUpdate(sql, params);
	}

}
